package joyi.train.employee.service.impl;


import j.framework.core.ServiceContext;
import j.util.StringUtils;
import j.util.jdbc.RowMapper;
import j.util.pkgenerator.PG;
import joyi.train.employee.domain.Employee;
import joyi.train.employee.service.EmployeeService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by WY2015092804 on 2015/10/19.
 */
public class EmployeeServiceImpl implements EmployeeService {

    @Override
    public Employee getEmployee(ServiceContext serviceContext) {
        String employeeId = serviceContext.params.getString("employeeId");
        if (StringUtils.isNotEmpty(employeeId)) {
            return serviceContext.getJdbcTemplate().queryForDomain("select * from cw_Employee where employeeId=?", new RowMapper<Employee>() {
                @Override
                public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Employee employee = new Employee();
                    employee.setDepartmentId(rs.getString("departmentId"));
                    employee.setNote(rs.getString("note"));
                    employee.setBirthday(rs.getDate("birthday"));
                    employee.setEmployeeId(rs.getString("employeeId"));
                    employee.setSalary(rs.getDouble("salary"));
                    employee.setGender(rs.getInt("gender"));
                    employee.setMarry(rs.getInt("marry"));
                    employee.setPositionId(rs.getString("positionId"));
                    employee.setEmployeeName(rs.getString("employeeName"));
                    return employee;
                }
            },employeeId);
        }
        Employee employee = new Employee();
        String departmentId = serviceContext.params.getString("departmentId");
        if (StringUtils.isNullOrEmpty(departmentId)) {
            Object obj = serviceContext.getJdbcTemplate().queryForMap("select departmentId from cw_Department where parentDepartmentId is null").get("departmentId");
            if (obj != null){
                employee.setDepartmentId(obj.toString());
            }
        }else {
            employee.setDepartmentId(departmentId);
        }
        try {
            employee.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2000-01-01"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return employee;
    }

    @Override
    public String delEmployee(ServiceContext serviceContext) {
        String employeeId = serviceContext.params.getString("employeeId");
        if (StringUtils.isNotEmpty(employeeId)){
            int de = serviceContext.getJdbcTemplate().execute("delete from cw_employee where employeeId=?",employeeId);
            return de==0?"此员工已经被移除":"删除成功";
        }
        serviceContext.actionResult.add("err","没有指定要删除的员工");
        return null;
    }

    @Override
    public void saveOrUpdateEmployee(ServiceContext serviceContext) {
        Employee employee = serviceContext.getBean(Employee.class);
        int ret;
        if (StringUtils.isNotEmpty(employee.getEmployeeId())){
            ret = serviceContext.getJdbcTemplate().update(employee);
        }else {
            employee.setAddedUserId(serviceContext.getUser().getUserId());
            employee.setAddedTime(new Date());
            employee.setEmployeeId(PG.getNext("EMP"));
            ret = serviceContext.getJdbcTemplate().insert(employee);
        }
        if(ret == -1){

        }
    }
}
